<template>
  <div>
    <el-form :inline="true" :model="state.formInline" class="demo-form-inline">
      <el-form-item label="商品名称">
        <el-input v-model="state.formInline.key" placeholder="模糊搜索"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="addOrEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="state.tableData" style="width: 100%">
      <el-table-column prop="name" label="名称" min-width="180" />
      <el-table-column prop="price" label="价格" min-width="180" />
      <el-table-column prop="desc" label="描述" min-width="180" />
      <el-table-column prop="typename" label="类别" min-width="180" >
        <template #default="scope">
          <span>{{ scope.row.typename || scope.row.foodtypes[0].name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="img" label="图片" min-width="180">
        <template #default="scope">
          <img class="goodImg" :src="scope.row.img" alt="">
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="200">
        <template #default="scope">
          <el-button size="mini" @click="addOrEdit(scope.row)">编辑</el-button>
          <el-popconfirm
            confirm-button-text="是"
            cancel-button-text="否"
            :icon="InfoFilled"
            icon-color="red"
            title="你确定要删除此项吗？"
            @confirm="remove(scope.row._id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="state.page.pageNo"
      :page-size="state.page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="state.page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <AddUpdate @reload="getList" ref="AddUpdateRef"></AddUpdate>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, ref, unref } from 'vue'
import { goodsPage, goodsDel } from '@/api/goods'
import { ElMessage } from 'element-plus'
import AddUpdate from './GoodsAddUpdate.vue'
// import UsersRole from './UsersRole.vue'
import { InfoFilled } from '@element-plus/icons-vue'

export default defineComponent({
  name: 'goods',
  setup() {
    const AddUpdateRef = ref()
    // reactive定义多个数据的响应式
    const state = reactive({
      formInline: {
        key: ''
      },
      tableData: [],
      page: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      }
    })

    const getList = () => {
      const { pageNo, pageSize } = state.page
      const { key } = state.formInline
      goodsPage({ pageNo, pageSize, key }).then((res) => {
        if (res.data.code === 200) {
          const temp = res.data
          const { data, pageNo, pageSize, total } = temp
          state.tableData = data
          state.page = {
            pageNo,
            pageSize,
            total
          }
        }
      })
    }

    const onSubmit = () => {
      state.page.pageNo = 1
      getList()
    }

    const handleSizeChange = (val: number) => {
      state.page.pageSize = val
      getList()
    }

    const handleCurrentChange = (val: number) => {
      state.page.pageNo = val
      getList()
    }

    const addOrEdit = (row: object) => {
      unref(AddUpdateRef).init(row)
    }

    const remove = (_id: string) => {
      goodsDel({ _id }).then((res) => {
        if (res.data.code === 200) {
          ElMessage.success('删除成功')
          getList()
        }
      })
    }

    onMounted(() => {
      getList()
    })

    return {
      state,
      onSubmit,
      handleSizeChange,
      handleCurrentChange,
      addOrEdit,
      getList,
      AddUpdateRef,
      remove,
      InfoFilled
    }
  },
  components: { AddUpdate }
})
</script>

<style lang="less" scoped>
.goodImg {
  width: 40px;
  height: 40px;
}
</style>
